package com.ly.hotel.broadcast;


import java.lang.ref.WeakReference;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyKeyEventCode;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.util.DensityUtil;
import com.ly.hotel.view.common.CustomDialog;

import de.greenrobot.event.EventBus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

/**
 * @Description: 通过BroadcastReceiver重启当前页面，实现语言切换
 * @author HanleyTowne
 * @email  tanghly@gmail.com
 * @date   2012-7-27下午6:05:50
 * @Google+ http://gplus.to/hanleytowne 
 */
public class VirtualKeyReceiver extends BroadcastReceiver {
	private MyApplication app = (MyApplication) MyApplication.getContext();
	public ThisHandler handlerMsg = new ThisHandler(this);
	@Override
	public void onReceive(Context context, Intent intent) {
		int key = intent.getIntExtra("key", 0);
		Log.i("VirtualKeyReceiver", "收到虚拟按键按键->" + key);
		if(key == MyKeyEventCode.KEYCODE_CALL){
			if(app.isBtnItemEnable("呼叫")){
				call();
			}
		}
	}
	
	public void call() {
		Log.i("MyBaseActivity", "初始化呼叫确定框");
		String callTips="";
		if(app.getCallStatus() == Constants.CALL_CANCLE){
			callTips = MyApplication.getContext().getString(R.string.call_warning_sure);
		}else if(app.getCallStatus() == Constants.CALL_REQUEST){
			callTips = MyApplication.getContext().getString(R.string.call_warning_cancle);
		}
		
		CustomDialog.Builder builder = new CustomDialog.Builder(MyApplication.getContext()).setTitle(callTips)
				.setFirstButton(MyApplication.getContext().getString(R.string.WarningYes), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface di, int arg1) {
						
//						if (app.isService()) {
							new Thread(new Runnable() {
								@Override
								public void run() {
									String rv = "";
									rv = app.changeLight();
									Message msg = new Message();
									Bundle data = new Bundle();
									data.putString("msg", rv);
									msg.setData(data);
									msg.what = 4;
									handlerMsg.sendMessage(msg);
								}

							}).start();
							
							if(app.getCallStatus() == Constants.CALL_CANCLE){
								EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_CALL_PREFIX + "呼叫"));
							}else{
								EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_CALL_PREFIX + "取消呼叫"));
							}
							
							di.dismiss();
//						}
//						Toast.makeText(getBaseContext(), "请求成功", Toast.LENGTH_LONG).show();
					}
				}).setSecondButton(MyApplication.getContext().getString(R.string.WarningNo), new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface di, int arg1) {
						di.dismiss();
					}
				});

		CustomDialog ad = builder.create();
		ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		ad.show();
		ad.getWindow().setLayout(DensityUtil.dip2px(300), DensityUtil.dip2px(200));
	}
	
	public static class ThisHandler extends Handler {
		private WeakReference<VirtualKeyReceiver> wrActivity;

		public ThisHandler(VirtualKeyReceiver virtualKeyReceiver) {
			wrActivity = new WeakReference<VirtualKeyReceiver>(virtualKeyReceiver);
		}

		@Override
		public void handleMessage(Message message) {
			VirtualKeyReceiver act = wrActivity.get();
			if (act != null) {
				switch (message.what) {
					case 4:
						String jsonStr = message.getData().getString("msg");
						if(jsonStr != null && !jsonStr.equals("")){
							Toast.makeText(MyApplication.getContext(), jsonStr, Toast.LENGTH_SHORT).show();
						}
					}
			}
		}
	}
}
